﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

using DaphneTranslator.BusinessLogic;
using DaphneTranslatorWPF.Mvvm;
using DaphneTranslatorWPF.View;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using DaphneTranslatorWPF.Common;

namespace DaphneTranslatorWPF.ViewModel
{
    /// <summary>
    /// View model for the tree
    /// </summary>
    public class TreeDataViewModel: INotifyPropertyChanged
    {
        public TreeDataViewModel()
        {
            Items = new ObservableCollection<TreeDataViewModel>();
        }         

        /// <summary>
        /// Sub items
        /// </summary>
        public ObservableCollection<TreeDataViewModel> Items
        { get; set; }

        
        /// <summary>
        /// Title of the tree item
        /// </summary>
        public string Title
        {
            get
            {
                if (Data is CultureCollection)
                    return "Root";
                else if (Data is CultureRecord)
                {
                    CultureRecord cr = Data as CultureRecord;
                    return string.Format("{0} ({1}/{2})", cr.Culture, cr.EnglishName, cr.CultureName);
                }
                else if (Data is LanguageFile)
                {
                    return (Data as LanguageFile).Filename;
                }

                return string.Empty;
            }
        }

        public bool IsPrimaryCulture
        { get; set; }

        /// <summary>
        /// Data object
        /// </summary>
        public object Data
        { get; set; }

        public List<ContextMenuItem> ContextMenuItemList
        {
            get
            {
                List<ContextMenuItem> lstItems = new List<ContextMenuItem>();
                if (Data is CultureCollection)
                {
                    lstItems.Add(new ContextMenuItem("Add new culture", AddNewCulture));
                }
                else if(Data is CultureRecord)
                {
                    lstItems.Add(new ContextMenuItem("Edit culture properties", EditCultureProperties));
                    lstItems.Add(new ContextMenuItem("Set as primary culture", SetAsPrimaryCultureContextMenu));
                    lstItems.Add(new ContextMenuItem("Add new file", AddNewFile));
                }
                else if(Data is LanguageFile)
                {
                    lstItems.Add(new ContextMenuItem("Edit file properties", EditFileProperties));
                }

                return lstItems;
            }
        }


        /// <summary>
        /// Converts model to the tree view model
        /// </summary>
        public static ObservableCollection<TreeDataViewModel> ParseCultureCollection(CultureCollection cultureCollection)
        {
            ObservableCollection<TreeDataViewModel> collection = new ObservableCollection<TreeDataViewModel>();
            TreeDataViewModel rootModel = new TreeDataViewModel();
            rootModel.Data = cultureCollection;
            collection.Add(rootModel);

            foreach (var culture in cultureCollection.CultureList)
            {
                TreeDataViewModel cultureModel = CreateCultureModel(culture);

                foreach (var file in culture.Files)
                {
                    TreeDataViewModel fileModel = new TreeDataViewModel();
                    fileModel.Data = file;

                    cultureModel.Items.Add(fileModel);
                }

                rootModel.Items.Add(cultureModel);
            }

            return collection;        
        }

        public static TreeDataViewModel CreateCultureModel(CultureRecord cr)
        {
            TreeDataViewModel cultureModel = new TreeDataViewModel();
            cultureModel.Data = cr;
            Messenger.Default.Register<PrimaryLanguageSelectionMessage>(cultureModel, cultureModel.SetAsPrimaryCulture);
            return cultureModel;
        }

        protected void AddNewCulture(object obj)
        {
            CultureSelectionDialog dlg = new CultureSelectionDialog();
            dlg.Owner = Application.Current.MainWindow;
            if (dlg.ShowDialog().Value == true)
            {
                CultureSelectionViewModel vm = dlg.DataContext as CultureSelectionViewModel;
                CultureCollection coll = Data as CultureCollection;

                CultureRecord cr = coll.CreateNewCulture(vm.SelectedCulture, vm.SelectedCulture, vm.SelectedCulture);

                TreeDataViewModel tdvm = CreateCultureModel(cr);
                Items.Add(tdvm);

                OnPropertyChanged("Items");
                tdvm.OnPropertyChanged("Title");                                  
            }
        }

        protected void EditCultureProperties(object obj)
        {
            CultureRecord cr = Data as CultureRecord;

            CulturePropertiesDialog dlg = new CulturePropertiesDialog();
            dlg.Owner = Application.Current.MainWindow;

            CulturePropertiesViewModel wm = new CulturePropertiesViewModel();
            wm.EnglishName = cr.EnglishName;
            wm.LocalizedName = cr.CultureName;
            dlg.DataContext = wm;

            if (dlg.ShowDialog().Value == true)
            {
                cr.CultureName = wm.LocalizedName;
                cr.EnglishName = wm.EnglishName;

                OnPropertyChanged("Title");
            }
        }

        protected void AddNewFile(object obj)
        {
            CultureRecord cr = Data as CultureRecord;

            NewFileDialog dlg = new NewFileDialog();
            dlg.Owner = Application.Current.MainWindow;

            NewFileDialogViewModel wm = new NewFileDialogViewModel();
            wm.CreateStrategy = enumCreationStrategy.NewAssociation;
            wm.PathViewModel.RootPath = Parameters.Default.GetStringValue(ParamKeys.keyRootPath);

            string sPrimaryCultureCode = Parameters.Default.GetStringValue(ParamKeys.keyPrimaryCultureCode);
            MappedFileCollection fileCollection = cr.Parent.FileMapperContainer.GetCollectionByCultureCode(sPrimaryCultureCode);
            if(fileCollection != null)
                wm.ExistingFiles = fileCollection.ToList();

            dlg.DataContext = wm;

            if (dlg.ShowDialog().Value == true)
            {
                LanguageFile file = new LanguageFile();
                file.Culture = file.CultureName = file.EnglishName = cr.Culture;
                file.Filename = wm.FileName;
                file.RelativePath = wm.PathViewModel.EnteredPath;
                cr.AddFile(file);

                TreeDataViewModel fileModel = new TreeDataViewModel();
                fileModel.Data = file;
                this.Items.Add(fileModel);
                OnPropertyChanged("Items");
            }   
        }

        protected void EditFileProperties(object obj)
        {
            LanguageFile file = Data as LanguageFile;

            FilePropertiesDialog dlg = new FilePropertiesDialog();
            dlg.Owner = Application.Current.MainWindow;

            FilePropertiesViewModel wm = new FilePropertiesViewModel();
            wm.FileName = file.Filename;
            wm.PathSelectionViewModel.RootPath = Parameters.Default.GetStringValue(ParamKeys.keyRootPath);
            wm.PathSelectionViewModel.EnteredPath = file.RelativePath;
            wm.RenamingStrategy = enumRenamingStrategy.CreateNewGroup;

            dlg.DataContext = wm;
            if (dlg.ShowDialog().Value == true)
            {
            }
        }

        protected void SetAsPrimaryCulture(PrimaryLanguageSelectionMessage message)
        {
            if (Data is CultureRecord)
            {
                CultureRecord cr = Data as CultureRecord;

                IsPrimaryCulture = message.PrimaryCultureCode == cr.Culture ? true : false;
                OnPropertyChanged("IsPrimaryCulture");
            }
        }

        protected void SetAsPrimaryCultureContextMenu(object obj)
        {
            if (Data is CultureRecord)
            {
                CultureRecord cr = Data as CultureRecord;
                PrimaryLanguageSelectionMessage message = new PrimaryLanguageSelectionMessage(cr.Culture);
                Messenger.Default.Send<PrimaryLanguageSelectionMessage>(message);
            }
        }


        public void OnPropertyChanged(string sProperty)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(sProperty));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
      
    }

    public class ContextMenuItem
    {
        public string Title
        { get; set; }

        public DelegateCommand<object> Command
        { get; set; }

        public ContextMenuItem(string sTitle, Action<object> commandAction)
        {
            Title = sTitle;
            Command = new DelegateCommand<object>(commandAction);
        }
    }
}
